package com.luzhikun.kickUser;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;
import java.util.Map;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class User implements HttpSessionBindingListener, Serializable {
    private String username;
    private int age;

    public User() {
    }

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "Username='" + username + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public void valueBound(HttpSessionBindingEvent event) {

        // 获取当前session
        HttpSession currenSession = event.getSession();
        // 从session中获取到全局域
        ServletContext sc = currenSession.getServletContext();
        // 从全局域中获取map
        Map<String, HttpSession> map = (Map<String, HttpSession>) sc.getAttribute("map");
        // 将当前用户名和session放入map中
        map.put(username, currenSession);


    }
}
